import json


class vod5g:
    def __init__(self, text):
        print(text)
        if text == 'Failed to retrieve the webpage: Status code 404':
            self.id = 0
        else:
            data = json.loads(text)
            if data["status"] == 200:
                print('ok')
                self.id = data["data"]['id']
                self.title = data["data"]["title"]
                self.pic = data["data"]["litpic"]
                self.typename = data["data"]["typename"]
                self.m3u8url = data["data"]["playurl"]
                self.tag = data["data"]["tag"]
                self.body = str.replace(data["data"]["body"],"'","\"")
                self.series = data["data"]["series"]
            else:
                self.id = 0

    def __str__(self):
        return (f'id:{self.id}\r\ntitle:{self.title}\r\npic:{self.pic}\r\ntypename:{self.typename}\r\n'
                f'm3u8url:{self.m3u8url}\r\ntag:{self.tag}\r\nbody:{self.body}\r\nseries:{self.series}')

    def insert(self):
        tagstr = ','.join(self.tag)
        return {"id": self.id, "title": self.title, "litpic": self.pic, "typename": self.typename,
                "m3u8url": self.m3u8url, "tag": tagstr, "body": self.body, "series": self.series}
